package by.ins.webconsole.jsf;

import java.util.Collection;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.FacesConverter;
import javax.faces.event.ValueChangeEvent;

import by.ins.app.AppContext;
import by.ins.life.evolution.ILifeEvoluter;

/**
 * @author Aliaksei_Miadzvedzeu
 *
 */
@ManagedBean(name = "lifeEvolutorsBean")
@RequestScoped
public class LifeEvolutors {
	
	
	@FacesConverter(value="LifeEvoluterNameConverter")
	public static class Converter implements  javax.faces.convert.Converter{

		@Override
		public Object getAsObject(FacesContext arg0, UIComponent arg1,
				String arg2) {
			ILifeEvoluter<?> le= AppContext.getDefiendLifeEvoluter(arg2);
			return le;
		}

		@Override
		public String getAsString(FacesContext arg0, UIComponent arg1,
				Object arg2) {
			//ILifeEvoluter<?> le = (ILifeEvoluter<?>) arg2;
			return (String)arg2;
		}
		
	}
	protected ILifeEvoluter<?> selectedLifeEvolutor;
	
	
/*
 *  return selected Life Evolutor. First LE by default. 
 */
	public ILifeEvoluter<?> getSelectedLifeEvolutor() {
		if ( selectedLifeEvolutor == null){
			javax.el.ELContext elContext = javax.faces.context.FacesContext.getCurrentInstance().getELContext();
			Object obj = javax.faces.context.FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "lifeEvolutorsBean");
			Object obj2 = javax.faces.context.FacesContext.getCurrentInstance().getApplication().getELResolver().getCommonPropertyType(elContext, "null");

			
			selectedLifeEvolutor = AppContext.getDefinedLifeEvolutor().get(0);
		}
		return selectedLifeEvolutor;
	}

	public void setSelectedLifeEvolutor(ILifeEvoluter<?> selectedLifeEvolutor) {
		this.selectedLifeEvolutor = selectedLifeEvolutor;
	}

	public Collection<ILifeEvoluter> getLifes() {
		List<ILifeEvoluter> rs = AppContext.getDefinedLifeEvolutor();

		return rs;
	}

	public void onSelectChanged(ValueChangeEvent e){
		this.selectedLifeEvolutor = (ILifeEvoluter<?>) e.getNewValue();

	}
	
	public String test() {
		return "Hello test";
	}

	public String getTest() {
		return "Hello grtTest";
	}

}
